package com.spzx.user.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.user.service.RegionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.api.domain.UserAddress;
import com.spzx.user.service.UserAddressService;

/**
 * 用户地址Service业务层处理
 *
 * @author shr
 * @since  2024-07-29
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService
{
    @Resource
    private UserAddressMapper userAddressMapper;
    @Resource
    private RegionService regionService;

    /**
     * 查询用户地址列表
     *
     * @return 用户地址
     */
    @Override
    public List<UserAddress> selectUserAddressList() {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getUserId, SecurityContextHolder.getUserId()));
    }

    @Override
    public int insertUserAddress(UserAddress userAddress) {
        userAddress.setUserId(SecurityContextHolder.getUserId());
        String provinceName = regionService.getNameByCode(userAddress.getProvinceCode());
        String cityName = regionService.getNameByCode(userAddress.getCityCode());
        String districtName = regionService.getNameByCode(userAddress.getDistrictCode());
        String fullAddress = provinceName + cityName + districtName + userAddress.getAddress();
        userAddress.setFullAddress(fullAddress);

        //如果是默认地址，其他地址改为非默认地址
        if (userAddress.getIsDefault() == 1){
            userAddressMapper.update(null, new LambdaUpdateWrapper<UserAddress>()
                    .eq(UserAddress::getUserId, SecurityContextHolder.getUserId())
                    .eq(UserAddress::getIsDefault, 1)
                    .set(UserAddress::getIsDefault, 0));
        }
        return userAddressMapper.insert(userAddress);
    }

    @Override
    public int updateUserAddress(UserAddress userAddress) {
        String provinceName = regionService.getNameByCode(userAddress.getProvinceCode());
        String cityName = regionService.getNameByCode(userAddress.getCityCode());
        String districtName = regionService.getNameByCode(userAddress.getDistrictCode());
        String fullAddress = provinceName + cityName + districtName + userAddress.getAddress();
        userAddress.setFullAddress(fullAddress);
        //如果是默认地址，其他地址更新为非默认地址
        if(userAddress.getIsDefault() == 1) {
            userAddressMapper.update(null, new LambdaUpdateWrapper<UserAddress>()
                    .eq(UserAddress::getUserId, userAddress.getUserId())
                    .eq(UserAddress::getIsDefault, 1)
                    .set(UserAddress::getIsDefault, 0)
            );
        }
        return userAddressMapper.updateById(userAddress);
    }

}
